package flc.ast.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.BatchPictureAdapter;
import flc.ast.databinding.ActivityBatchCompressBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.FileUtil;

public class BatchCompressActivity extends BaseAc<ActivityBatchCompressBinding> {

    public static List<String> listPath = new ArrayList<>();
    private BatchPictureAdapter pictureAdapter;
    private int rate = 10;
    private String format = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_batch_compress;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivBatchCompressBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvCompressRate1.setOnClickListener(this);
        mDataBinding.tvCompressRate2.setOnClickListener(this);
        mDataBinding.tvCompressRate3.setOnClickListener(this);
        mDataBinding.tvCompressRate4.setOnClickListener(this);
        mDataBinding.tvCompressRate5.setOnClickListener(this);
        mDataBinding.tvCompressRate6.setOnClickListener(this);
        mDataBinding.tvCompressFormat1.setOnClickListener(this);
        mDataBinding.tvCompressFormat2.setOnClickListener(this);
        mDataBinding.tvCompressFormat3.setOnClickListener(this);
        mDataBinding.tvCompressFormat4.setOnClickListener(this);
        mDataBinding.tvAddPic.setOnClickListener(this);
        mDataBinding.tvStartCompress.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new BatchPictureAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.addChildClickViewIds(R.id.ivBatchPictureItemDelete);
        pictureAdapter.setOnItemChildClickListener(this);
        pictureAdapter.setList(listPath);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()) {
            case R.id.tvAddPic://添加照片
                Intent intent = new Intent(this, SelPictureActivity.class);
                intent.putExtra(Content.Again, "Again");
                startActivityForResult(intent, Content.COMPRESS_ADD);
                break;
            //图片压缩率
            case R.id.tvCompressRate1://10%
                clearRateSelection();
                mDataBinding.tvCompressRate1.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 10;
                break;
            case R.id.tvCompressRate2://30%
                clearRateSelection();
                mDataBinding.tvCompressRate2.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 30;
                break;
            case R.id.tvCompressRate3://45%
                clearRateSelection();
                mDataBinding.tvCompressRate3.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 45;
                break;
            case R.id.tvCompressRate4://60%
                clearRateSelection();
                mDataBinding.tvCompressRate4.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 60;
                break;
            case R.id.tvCompressRate5://75%
                clearRateSelection();
                mDataBinding.tvCompressRate5.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 75;
                break;
            case R.id.tvCompressRate6://100%
                clearRateSelection();
                mDataBinding.tvCompressRate6.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 100;
                break;
            //图片格式
            case R.id.tvCompressFormat1://原格式
                clearFormatSelection();
                mDataBinding.tvCompressFormat1.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = getString(R.string.format_text1);
                break;
            case R.id.tvCompressFormat2://jpg
                clearFormatSelection();
                mDataBinding.tvCompressFormat2.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = getString(R.string.unit_jpg);
                break;
            case R.id.tvCompressFormat3://png
                clearFormatSelection();
                mDataBinding.tvCompressFormat3.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = getString(R.string.unit_png);
                break;
            case R.id.tvCompressFormat4://webp
                clearFormatSelection();
                mDataBinding.tvCompressFormat4.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = getString(R.string.unit_webp);
                break;
            case R.id.tvStartCompress://开始压缩
                if (listPath.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_pic));
                    return;
                }
                save();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivBatchPictureItemDelete:
                listPath.remove(position);
                pictureAdapter.setList(listPath);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void save() {
        mDataBinding.tvStartCompress.setEnabled(false);
        for (String s : listPath) {
            byte[] bytes = ImageUtils.compressByQuality(ImageUtils.getBitmap(s), rate);
            Bitmap bitmap = ImageUtils.bytes2Bitmap(bytes);
            Bitmap.CompressFormat compressFormat;
            String itemFormat;
            if (format.equals(getString(R.string.format_text1))) {
                itemFormat = "." + FileUtils.getFileExtension(s);
            } else {
                itemFormat = format;
            }
            if (itemFormat.equals(getString(R.string.unit_jpg))) {
                compressFormat = Bitmap.CompressFormat.JPEG;
            } else if (itemFormat.equals(getString(R.string.unit_png))) {
                compressFormat = Bitmap.CompressFormat.PNG;
            } else if (itemFormat.equals(getString(R.string.unit_webp))) {
                compressFormat = Bitmap.CompressFormat.WEBP;
            } else {
                compressFormat = Bitmap.CompressFormat.PNG;
            }
            //保存到相册
            ImageUtils.save2Album(bitmap, compressFormat);
            //保存到最近文件
            String filePath = FileUtil.generateFilePath(Content.myCompress, itemFormat);
            ImageUtils.save(bitmap, filePath, compressFormat);
        }
        mDataBinding.tvStartCompress.setEnabled(true);
        CompressActivity.isSingle = false;
        CompressActivity.listPath = listPath;
        startActivity(CompressActivity.class);
    }

    private void clearRateSelection() {
        mDataBinding.tvCompressRate1.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate2.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate3.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate4.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate5.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate6.setBackgroundColor(Color.parseColor("#03091B"));
    }

    private void clearFormatSelection() {
        mDataBinding.tvCompressFormat1.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressFormat2.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressFormat3.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressFormat4.setBackgroundColor(Color.parseColor("#03091B"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String path = data.getStringExtra(Content.imagePath);
            if (requestCode == Content.COMPRESS_ADD) {
                listPath.add(0, path);
                pictureAdapter.setList(listPath);
            }
        }
    }

}